package com.china08.yxyapi.api.manage.tec.office.attendance;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.attendance.YxyTeaAttendance;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.attendance.YxyTeaAttendanceRepository;
import com.china08.yxyapi.util.DateToolUtils;

/**
 * 教师考勤--今日考勤--今日考勤记录
 * 
 * @author LiuWeidong
 *
 */
@CrossOrigin
@RestController
public class AttendanceYxyTecAttendanceTodaySrhController extends YxyApiBaseController {

    @Autowired
    private YxyTeaAttendanceRepository yxyTeaAttendanceRepository;

    @RequestMapping(value = "/api_yw/manage/tec/office/attendance/today/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> precess(HttpServletRequest request, HttpServletResponse response) {
        String teacherId = getProfile(request, response).getId();
        String schoolId = getProfile(request, response).getSchoolId();

        // 获取当天日期
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        Integer dayOfDate = Integer.valueOf(dateFormat.format(date));

        List<RespModel> returnList = new ArrayList<>();

        // 查询今日打卡时间
        List<YxyTeaAttendance> yxyTeaAttendanceLst = yxyTeaAttendanceRepository
                .findByDayOfDateAndTeacherIdAndSchoolIdOrderByCreatedDateDesc(dayOfDate, teacherId, schoolId);
        int count = yxyTeaAttendanceLst.size();
        for (YxyTeaAttendance yxyTeaAttendance : yxyTeaAttendanceLst) {
            RespModel model = new RespModel();
            String sinTime = DateToolUtils.format(yxyTeaAttendance.getSignTime(), "HH:mm");
            model.signTime = sinTime;
            model.signCount = count;
            returnList.add(model);
            count--;
        }

        return returnList;
    }

    /* 请求参数 */
    protected static class ReqModel extends YxyApiPageReqModel {

    }

    /* 响应参数 */
    protected static class RespModel extends YxyApiRespModel {

        // 签到时间
        public String signTime;
        // 打卡次数
        public Integer signCount;
    }

}
